//
//  HomeOneVC.m
//  MyStock
//
//  Created by 刘翔 on 2024/10/21.
//

#import "HomeOneVC.h"
#import "HomeOneCell.h"
#import "HomeOneModel.h"
#import "GLDetailVC.h"
#import "HomeHeaderCell.h"


@interface HomeOneVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *dataList;

@property (nonatomic,strong) HomeOneModel *model;
@property(nonatomic, strong) dispatch_source_t timer;


@end

@implementation HomeOneVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //屠龙刀页面
    self.view.backgroundColor = [UIColor whiteColor];
    self.customNavBar.hidden = YES;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.mj_header = [LxResfreshHeader headerWithRefreshingBlock:^{
        [self loadData];
      }];
    [self.tableView.mj_header beginRefreshing];
    [self timerRefreshData];

}

- (void)timerRefreshData{
        
    //0.创建队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, 0);
    dispatch_source_set_timer(self.timer, startTime, 5 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    //3.要调用的任务
    dispatch_source_set_event_handler(self.timer, ^{
        [self loadData];
        NSLog(@"====屠龙刀");
    });
    
    //4.开始执行
    dispatch_resume(self.timer);
        
}

- (void)loadData{
    BADataEntity *entity = [BADataEntity new];
    entity.urlString = [NSString stringWithFormat:@"%@/%@",MYSTOCKUrl,@"hotgl"];
    entity.needCache = NO;
    entity.parameters = nil;
    [BANetManager ba_request_GETWithEntity:entity successBlock:^(id response) {
    
        NSDictionary *result = response;
        if ([result[@"code"] intValue] == 1) {
            self.model = [HomeOneModel mj_objectWithKeyValues:result[@"data"][@"datas"]];
            
            NSMutableArray *marray = [NSMutableArray array];
            for (NSString *key in self.model.topics) {
                HomeCellModel *model = [HomeCellModel mj_objectWithKeyValues:self.model.topics[key]];
                [marray addObject:model];
            }
            [marray sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
                HomeCellModel *model1 = obj1;
                HomeCellModel *model2 = obj2;

                return model1.weather_value < model2.weather_value;
            }];
            
            self.dataList = marray;
            
        }else{
            self.model = nil;
        }
       
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
    } failureBlock:^(NSError *error) {
        
        [self.tableView.mj_header endRefreshing];

    } progressBlock:^(int64_t bytesProgress, int64_t totalBytesProgress) {
        
    }];
}


#pragma  mark --------UITableView Delegete----------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.model == nil) {
        return 0;
    }
    return self.dataList.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        static NSString *identifire = @"HomeHeaderCellID";
        HomeHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:identifire];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeHeaderCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
        
        cell.dWeatherL.attributedText = [[NSAttributedString alloc] initWithString:self.model.weather];
        cell.upL.text = self.model.zt;
        cell.downL.text = self.model.dt;
        cell.dupL.text = self.model.ls_up;
        cell.ddownL.text = self.model.ls_down;
        cell.cupL.text = self.model.cyb_up;
        cell.cdownL.text = self.model.cyb_down;

                    
        return cell;
    }else{
        static NSString *identifire = @"HomeOneCellID";
        HomeOneCell *cell = [tableView dequeueReusableCellWithIdentifier:identifire];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeOneCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
        HomeCellModel *model = self.dataList[indexPath.row - 1];
        cell.nameL.text = model.name;
        cell.redL.attributedText = [[NSAttributedString alloc] initWithString:model.weather];
        cell.upL.text = model.up;
        cell.downL.text = model.down;
        return cell;
        
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 0.1;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row > 0) {
        HomeCellModel *model = self.dataList[indexPath.row -1];
        HotGLVC *vc = [[HotGLVC alloc] init];
        vc.glName = model.name;
        [self.navigationController pushViewController:vc animated:YES];
    }
   
        
}

#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView {
    return self.view;
}

- (void)listDidAppear {
    
}

- (void)listDidDisappear {
    
}



@end
